﻿/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the examples of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:BSD$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** BSD License Usage
** Alternatively, you may use this file under the terms of the BSD license
** as follows:
**
** "Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions are
** met:
**   * Redistributions of source code must retain the above copyright
**     notice, this list of conditions and the following disclaimer.
**   * Redistributions in binary form must reproduce the above copyright
**     notice, this list of conditions and the following disclaimer in
**     the documentation and/or other materials provided with the
**     distribution.
**   * Neither the name of The Qt Company Ltd nor the names of its
**     contributors may be used to endorse or promote products derived
**     from this software without specific prior written permission.
**
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** $QT_END_LICENSE$
**
****************************************************************************/

import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Controls.Material 2.0
import QtQuick.Controls.Styles 1.4
import QtQuick.Layouts 1.12
import QtGraphicalEffects 1.0
import QtQuick.Window 2.2
import "qrc:/MaterialUI/"
import "qrc:/MaterialUI/Interface/"
import "+material"

Page {
    property int staticLeftWidth: 300
    property var messagepool: ({})
    header: ToolBar {
        height: 65
        Rectangle{
            id:warning
            anchors{
                left: parent.left
                top: parent.top
                bottom: parent.bottom
            }
            width: 2
            visible: false
            color: "red"
        }

        Timer{
            id:warningTimer
            interval: 1000;
            running: false;
            repeat: true
            onTriggered:warning.visible=!warning.visible
        }

        Image {
            id: myimg
            anchors.left: parent.left
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            width: height
            anchors.margins: 10
            source: "qrc:/"+ChatManage.MyInfo["user_img"]+".png"
            layer.enabled: true
            layer.effect:DropShadow {
                horizontalOffset: 0
                verticalOffset: 0
                radius: 4
                samples: 9
                color: "#000000"
                spread:0
            }
        }
        MaterialLabel {
            id:title_name
            text: ChatManage.MyInfo["user_name"]
            font.pixelSize: 22
            anchors.horizontalCenter: parent.horizontalCenter
            verticalAlignment: Label.AlignBottom
            anchors.top: parent.top
            anchors.left: myimg.right
            anchors.leftMargin: 10
            height: parent.height/2+5
            width: parent.width/3
            layer.enabled: true
            layer.effect:DropShadow {
                horizontalOffset: 0
                verticalOffset: 0
                radius: 4
                samples: 9
                color: "#ffffff"
                spread:0
            }
        }
        MaterialLabel {
            text: ChatManage.MyInfo["user_mail"]
            font.pixelSize: 16
            height: parent.height/2
            color: "gray"
            anchors.horizontalCenter: parent.horizontalCenter
            verticalAlignment: Label.AlignTop
            anchors.top: title_name.bottom
            anchors.left: myimg.right
            anchors.leftMargin: 10
            width: parent.width/3
        }
        Rectangle{
            id:splitRec
            anchors.left: parent.left
            anchors.leftMargin: staticLeftWidth-1
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            width: 1
            color: "gray"
        }
        ListModel {
            id:menumodel
            ListElement {
                modelData: "设 置"
            }
            ListElement {
                modelData: "退 出"
            }
        }

        MaterialLabel {
            id:inconversationwith
            text: ""
            font.pixelSize: 22
            //            anchors.horizontalCenter: parent.horizontalCenter
            anchors.top: parent.top
            anchors.bottom: parent.bottom
            anchors.left: splitRec.right
            anchors.right: parent.right
            horizontalAlignment: Label.AlignHCenter
            verticalAlignment: Label.AlignVCenter
            layer.enabled: true
            layer.effect:DropShadow {
                horizontalOffset: 0
                verticalOffset: 0
                radius: 4
                samples: 9
                color: "#ffffff"
                spread:0
            }
        }

        MouseArea {
            anchors.fill: parent
            acceptedButtons: Qt.LeftButton
            property point clickPos: "0,0"

            onPressed: {
                clickPos  = Qt.point(mouse.x,mouse.y)
                //console.log("mouse Pressed")
            }
            onPositionChanged: {
                var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
                window.setX(window.x+delta.x)
                window.setY(window.y+delta.y)
            }
            onReleased: {
                var delta = Qt.point(mouse.x-clickPos.x, mouse.y-clickPos.y)
                window.setX(window.x+delta.x)
                window.setY(window.y+delta.y)
            }
        }
        Rectangle{
            id:toolsBorder
            anchors.right: splitRec.left
            anchors.rightMargin: 10
            anchors.top: parent.top
            anchors.topMargin: 10
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 10
            color: "#00000000"
            width: height
            Image {
                id: tools
                anchors.fill: parent
                fillMode:Image.PreserveAspectFit
                source: "qrc:/tool.png"
                layer.enabled: true
                layer.effect:DropShadow {
                    horizontalOffset: 0
                    verticalOffset: 0
                    radius: 4
                    samples: 9
                    color: "#000000"
                    spread:0
                }
            }
            layer.enabled: true
            layer.effect:DropShadow {
                horizontalOffset: 0
                verticalOffset: 0
                radius: 4
                samples: 9
                color: "#ffffff"
                spread:0
            }
            PropertyAnimation {
                id: animation;
                target: toolsBorder;
                property: "scale";
                to: 1;
                duration: 100
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                onEntered: {
                    toolsBorder.scale = 1.1
                    animation.running = true
                }
                onExited: {
                    toolsBorder.scale = 0.9
                }
                onClicked: {
                    toolsBorder.scale = 1
                    if(settingdiog.showing||hideManageDialog.showing)
                        return
                    settingdiog.show("","",null)
                }
            }
        }
        //        MaterialMenuField{
        //            id:mainMenu
        //            anchors.right: splitRec.left
        //            anchors.top: parent.top
        //            width: 70
        //            textVerticalAlignment:Label.AlignVCenter
        //            textHorizontalAlignment:Label.AlignHCenter
        //            helperText:""
        //            model:menumodel
        //            Rectangle{
        //                id:toolsBorder
        //                anchors.right: parent.right
        //                anchors.rightMargin: 10
        //                anchors.top: parent.top
        //                anchors.topMargin: 10
        //                anchors.bottom: parent.bottom
        //                anchors.bottomMargin: 10
        //                color: "#00000000"
        //                width: height
        //                Image {
        //                    id: tools
        //                    anchors.fill: parent
        //                    fillMode:Image.PreserveAspectFit
        //                    source: "qrc:/tool.png"
        //                    layer.enabled: true
        //                    layer.effect:DropShadow {
        //                        horizontalOffset: 0
        //                        verticalOffset: 0
        //                        radius: 4
        //                        samples: 9
        //                        color: "#000000"
        //                        spread:0
        //                    }
        //                }
        //                layer.enabled: true
        //                layer.effect:DropShadow {
        //                    horizontalOffset: 0
        //                    verticalOffset: 0
        //                    radius: 4
        //                    samples: 9
        //                    color: "#ffffff"
        //                    spread:0
        //                }
        //                PropertyAnimation {
        //                    id: animation;
        //                    target: toolsBorder;
        //                    property: "scale";
        //                    to: 1;
        //                    duration: 100
        //                }
        //            }
        //            onMouseAreaType:{
        //                if(type==0){
        //                    toolsBorder.scale = 1.1
        //                    animation.running = true
        //                }
        //                else if(type==1){
        //                    toolsBorder.scale = 0.9
        //                }
        //                else
        //                    toolsBorder.scale = 1
        //            }

        //            onItemSelected:{
        //                //index,text
        //                if(settingdiog.showing||hideManageDialog.showing)
        //                    return
        //                if(index==0){//设置
        //                    ChatManage.runUploadFile()
        //                    settingdiog.show("","",null)
        //                }
        //                else if(index==1){//退出
        //                    ChatManage.exitCurrentUser()
        //                    delegateLoader.source = "LoginMain.qml"
        //                }
        //            }
        //        }
        Image {
            id: close
            anchors{
                right: parent.right
                rightMargin: 5
                top: parent.top
                topMargin: 5
            }
            width: 20
            height: width
            fillMode:Image.PreserveAspectFit
            source: "qrc:/close.png"
            scale:0.8
            layer.enabled: true
            layer.effect:DropShadow {
                horizontalOffset: 0
                verticalOffset: 0
                radius: 4
                samples: 9
                color: "#000000"
                spread:0
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                onEntered: {
                    close.scale = 1
                }
                onExited: {
                    close.scale = 0.8
                }
                onClicked: {
                    window.close()
                }
            }
        }
        Rectangle{
            id:min
            anchors{
                right: close.left
                rightMargin: 12
                top: close.top
            }
            width: close.width
            height: width
            color: "#00000000"
            Image {
                id:minImag
                scale: 0.8
                anchors.centerIn: parent
                anchors.fill: parent
                anchors.topMargin: 5
                anchors.bottomMargin: 5
                anchors.leftMargin: 2
                anchors.rightMargin: 2
//                fillMode:Image.PreserveAspectFit
                source: "qrc:/min.png"
                layer.enabled: true
                layer.effect:DropShadow {
                    horizontalOffset: 0
                    verticalOffset: 0
                    radius: 4
                    samples: 9
                    color: "#000000"
                    spread:0
                }
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                onEntered: {
                    minImag.scale = 1
                }
                onExited: {
                    minImag.scale = 0.8
                }
                onClicked: {
                    window.visibility = Window.Minimized
                }
            }
        }
    }


    Connections{
        target: ChatManage
        function onRegisteChanged(){
            if(ChatManage.Registe){
                warningTimer.stop()
                warning.visible = false
            }
            else{
                if(!warningTimer.running){
                    warningTimer.start()
                    warning.visible = true
                }
            }
        }
    }

    function initRootPage(){
        SqlContactModel.select()
        inconversationwith.text = "GROUP"
        SqlContactModel.clearMessageSum(0)
    }

    Component.onCompleted: {
        initRootPage()
    }

    RectangularGlow {
        z: -1
        anchors.fill: parent
        glowRadius: 6
        spread: 0.22
        color: "#20000000"
    }

    Connections{
        target: ChatManage
        function onLoginAnswer(value,errMsg){
            if(value){
                console.log("登录成功")
                SqlContactModel.updateCurrentUserMail(ChatManage.MyInfo["user_mail"])
                inconversationwith.text = "GROUP"
                SqlContactModel.clearMessageSum(0)
                SqlContactModel.select()
                SqlConversationModel.isGroup = true;
                SqlConversationModel.recipient = "GROUP";
                materialUI.showSnackbarMessage("重新登录成功")

            }
            else{
                console.log("登录失败",ChatManage.Registe)
                if(ChatManage.Registe)
                    return
                ChatManage.exitCurrentUser()
                delegateLoader.source = "LoginMain.qml"
                materialUI.showSnackbarMessage(errMsg)
            }
        }
    }

    Item {
        id: leftList
        anchors{
            left: parent.left
            top: parent.top
            bottom: parent.bottom
        }
        width: staticLeftWidth
        ContactPage {
            anchors.fill: parent
            onItemClicked:{
                messagepool[stackView.inConversationMail] = stackView.nowmessageField
                stackView.inConversationMail = mail
                stackView.inConversationWith = name
                SqlConversationModel.me = ChatManage.MyInfo["user_mail"]
                if(mail==="GROUP"){
                    SqlConversationModel.isGroup = true
                }
                else{
                    SqlConversationModel.isGroup = false
                }
                SqlConversationModel.recipient = mail

                inconversationwith.text = online==="0"?name+"(离线)":name

                SqlContactModel.currentIndex=index
                if(message!=="0")
                    SqlContactModel.clearMessageSum(index)
                if(messagepool.hasOwnProperty(stackView.inConversationMail))
                    stackView.updateNowmessage(messagepool[stackView.inConversationMail])
                else
                    stackView.updateNowmessage("")
                //                stackView.push("qrc:/ConversationPage.qml", { inConversationWith: name,inConversationMail:mail})
            }
        }
    }

    Rectangle{
        anchors.right: leftList.right
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        width: 1
        color: "gray"
    }

    Item {
        id: rightView
        anchors{
            left: leftList.right
            top: parent.top
            bottom: parent.bottom
            right: parent.right
        }
        ConversationPage{
            id: stackView
            anchors.fill: parent
            inConversationWith:"GROUP"
            inConversationMail:"GROUP"
        }
    }

    MaterialUI {
        id: materialUI
        anchors.fill: parent
        dialogCancelText: "取消"
        dialogOKText: "确定"
    }

    MaterialDialogAlert{
        id:settingdiog
        //        dialogContent:UserConfig{
        //            id:userconfig
        //            width: 460
        //            height: 280
        //        }
        dialogContent:UserConfigMain{
            id:userconfig
            width: 530
            height: 280
        }
        onOpened: {
            userconfig.initOpen()
        }
        onAccepted: {
            userconfig.saveNowConfig()

        }
    }

    MaterialDialogAlert{
        id:hideManageDialog
        positiveButtonText:"CLOSE"
        dialogContent:ManageControl{
            width: 460
            height: 280
        }
        onAccepted: {

        }
    }

    Action{
        shortcut: "Ctrl+Alt+K"
        onTriggered: {
            if(!settingdiog.showing)
                hideManageDialog.show("","",null)
        }
    }
}


